package com.sam.chat.controller;

import com.sam.chat.api.CommonResult;
import com.sam.chat.service.IChatGroupService;
import com.sam.chat.utils.RedisUtils;
import com.sam.chat.vo.GroupVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器 -群管理
 * </p>
 *
 * @author sam
 * @since 2023-01-31
 */
@Tag(name = "群管理")
@RestController
@RequestMapping("/group")
@RequiredArgsConstructor
@Component
public class ChatGroupController {
    private final IChatGroupService iChatGroupService;
    @Autowired
    private RedisUtils redisUtils;





    /**
     * 创建群 并 添加群成员
     */
    @Operation(summary = "创建群并添加群成员")
    @PostMapping("createGroup")
    public CommonResult<Boolean> createGroup(@RequestBody Map<String, Object> map) {
        boolean result = iChatGroupService.createGroup(map.get("groupName").toString(), map.get("uid").toString(), (ArrayList<String>) map.get("memberIdList"));
        return new CommonResult<>(200, "", result);
    }

    /**
     * 删除群（解散群）
     * 需要前端判断是不是群存在，不然会有垃圾redis
     */
    @Operation(summary = "删除群（解散群）")
    @PostMapping("deleteGroup")
    public CommonResult<Boolean> deleteGroup(@RequestParam String groupId) {
        boolean result = iChatGroupService.deleteGroup(groupId);
        return new CommonResult<>(200, "", result);
    }

    /**
     * 退出群
     * 需要前端判断是不是群存在，不然会有垃圾redis
     */
    @Operation(summary = "退出群")
    @PostMapping("exitGroup")
    public CommonResult<Boolean> exitGroup(@RequestParam String groupId, @RequestParam String userId) {
        boolean result = iChatGroupService.exitGroup(groupId,userId);
        return new CommonResult<>(200, "", result);
    }

    /**
     * 删除群成员
     */
    @Operation(summary = "删除群成员")
    @PostMapping("deleteGroupMember")
    public CommonResult<Boolean> deleteGroupMembers(@RequestParam String groupId, @RequestParam String userId) {
        boolean result = iChatGroupService.exitGroup(groupId,userId);
        return new CommonResult<>(200, "", result);
    }

    /**
     * 修改群名称
     */
    @Operation(summary = "修改群名称")
    @PostMapping("modifyGroupName")
    public CommonResult<Boolean> modifyGroupName(String groupId, String name) {
        boolean result = iChatGroupService.modifyGroupName(groupId,name);
        return new CommonResult<>(200, "", result);
    }



    /**
     * 获取群成员
     */
    @Operation(summary = "获取群成员")
    @PostMapping("getGroupMembers")
    public CommonResult<Boolean> getGroupMember(@RequestParam String groupId) {
        boolean result = iChatGroupService.deleteGroup(groupId);
        return new CommonResult<>(200, "", result);
    }

    /**
     * 添加群成员
     */
    @Operation(summary = "添加群成员")
    @PostMapping("addGroupMembers")
    public CommonResult<Boolean> addGroupMembers(@RequestParam Map<String, Object> map) {
       ArrayList<String> memberIdList = (ArrayList<String>) map.get("memberIdList");
       String groupId = (String) map.get("groupId");
       boolean result = iChatGroupService.addGroupMembers(groupId,memberIdList);
       return new CommonResult<>(200, "", true);
    }

    /**
     * 我的群列表
     */
    @Operation(summary = "我的群列表")
    @GetMapping("getMyGroupList")
    public CommonResult<List<GroupVo>> getMyGroupList(String userId) {
        List<GroupVo> data = iChatGroupService.getGroupList(userId);
        return new CommonResult<>(200, "", data);
    }


}

